package point24.util;

import java.util.*;

public class AISearcherImp implements Searcher {


    public static void main(String[] args) {
        System.out.println(new AISearcherImp().search(new Integer[]{3, 2, 3, 6}));
        // System.out.println(search(new int[]{1, 4, 2, 3}));
        //  System.out.println(search(new int[]{1, 1, 1, 1}));
    }


    @Override
    public List<String> search(Integer[] arr) {
        Set<String> resultList = new TreeSet<>();

        for (int cc = 0; cc < MathTools.FAC[4]; cc++) {
            int[] idx = MathTools.codel(cc, 4);
            String a = String.valueOf(arr[idx[0]]);
            String b = String.valueOf(arr[idx[1]]);
            String c = String.valueOf(arr[idx[2]]);
            String d = String.valueOf(arr[idx[3]]);
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    for (int k = 0; k < 4; k++) {
                        String X = OPERATORS[i];
                        String Y = OPERATORS[j];
                        String Z = OPERATORS[k];
                        for (String[] ee : new String[][]{
                                {a, b, X, c, Y, d, Z},
                                {a, b, c, X, Y, d, Z},
                                {a, b, X, c, d, Y, Z},
                                {a, b, c, X, d, Y, Z},
                                {a, b, c, d, X, Y, Z},}) {
                            if (Math.abs(ReversePolishNotation.eval(ee) - TARGET) < EPS) {
                                resultList.add(BinaryTree.convertRpnToNormalExpression(Arrays.asList(ee)));
                            }
                        }
                    }
        }
        if (resultList.isEmpty()) return Collections.singletonList("No solution");
        return new Vector<>(resultList);
    }


}
